package org.ricks.conditon;

import org.ricks.ioc.event.IEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

// 组合条件实现
public class CompositeCondition implements Condition {
    private final List<Condition> conditions;
    private final CompleteType completeType;

    public CompositeCondition(List<Condition> conditions, CompleteType completeType) {
        this.conditions = List.copyOf(conditions); // 不可变列表
        this.completeType = completeType;
    }

    @Override
    public void emit(IEvent event) {
        conditions.forEach(c -> c.emit(event));
    }

    @Override
    public boolean isDone() {
        return completeType == CompleteType.ALL ?
                conditions.stream().allMatch(Condition::isDone) :
                conditions.stream().anyMatch(Condition::isDone);
    }

    public enum CompleteType { ANY, ALL }
}
